package com.hejie.videoservice.config;

import com.hejie.videoservice.dto.VideoEventDTO;
import com.hejie.videoservice.service.VideoService;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.support.serializer.JsonDeserializer;

import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableKafka
public class KafkaConsumerConfig {

    @Value("${spring.kafka.bootstrap-servers}")
    private String bootstrapServers;

    @Value("${spring.kafka.consumer.group-id:video-service-group}")
    private String groupId;

    @Value("${spring.kafka.consumer.auto-offset-reset:earliest}")
    private String autoOffsetReset;

    @Value("${spring.kafka.consumer.concurrency:3}")
    private int concurrency;

    private final VideoService videoService;

    public KafkaConsumerConfig(VideoService videoService) {
        this.videoService = videoService;
    }

    @Bean
    public ConsumerFactory<String, Object> consumerFactory() {
        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, autoOffsetReset);
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
        props.put(JsonDeserializer.TRUSTED_PACKAGES, "com.hejie.videoservice.dto");
        return new DefaultKafkaConsumerFactory<>(props);
    }

    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, Object> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, Object> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        factory.setConcurrency(concurrency);
        return factory;
    }

    @KafkaListener(topics = KafkaProducerConfig.TOPIC_VIDEO_UPLOADED, groupId = "${spring.kafka.consumer.group-id}")
    public void handleVideoUploadedEvent(VideoEventDTO event) {
        videoService.handleVideoUploadedEvent(event);
    }

    @KafkaListener(topics = KafkaProducerConfig.TOPIC_VIDEO_PROCESSED, groupId = "${spring.kafka.consumer.group-id}")
    public void handleVideoProcessedEvent(VideoEventDTO event) {
        videoService.handleVideoProcessedEvent(event);
    }

    @KafkaListener(topics = KafkaProducerConfig.TOPIC_VIDEO_DELETED, groupId = "${spring.kafka.consumer.group-id}")
    public void handleVideoDeletedEvent(VideoEventDTO event) {
        videoService.handleVideoDeletedEvent(event);
    }

    @KafkaListener(topics = KafkaProducerConfig.TOPIC_VIDEO_STATUS_CHANGED, groupId = "${spring.kafka.consumer.group-id}")
    public void handleVideoStatusChangedEvent(VideoEventDTO event) {
        videoService.handleVideoStatusChangedEvent(event);
    }
}